/** \file
* Implementation of the generic Twitter database class.
* This file is part of Chorus, available at
* http://code.google.com/p/chorus-twitter/ . This file and all associated files
* and derivative works are bound by the terms of the LGPL 3.0 license,
* available at http://www.gnu.org/licenses/lgpl-3.0-standalone.html .
* \par Subversion URL:
* \$URL: http://chorus-twitter.googlecode.com/svn/branches/cpp/src/xml/XmlTree.cpp $
* \version \$Rev: 153 $
* \date    \$Date: 2010-04-06 07:43:42 +0000 (Tue, 06 Apr 2010) $
*/

#include "config.h"

#include <string>

#include "system/DebugConsole.hpp"

#include "xml/XmlTree.hpp"

using namespace std;

XmlTree::XmlTree() :
	threadload  (*this, &XmlTree::load_block),
	threadupdate(*this, &XmlTree::update_block),
	threadsave  (*this, &XmlTree::save_block)
{
	currentProp = 0;
}

XmlTree::~XmlTree()
{
}

bool XmlTree::load()
{
	return threadload.start();
}

bool XmlTree::update()
{
	return threadupdate.start();
}

bool XmlTree::save()
{
	return threadsave.start();
}

bool XmlTree::load_block()
{
	return loadPerform();
}

bool XmlTree::update_block()
{
	return updatePerform();
}

bool XmlTree::save_block()
{
	return savePerform();
}

void XmlTree::startElement(const xmlChar *name, const xmlChar **attrs)
{
	if (xmlStrEqual(getElementTag(), name))
		newPendingElement();
	else
	{
		XmlElement *const pending = getPendingElement();
		if (pending)
		{
			currentProp = pending->getProp((const char*)name);
			propText.clear();
		}
	}
}

void XmlTree::endElement(const xmlChar *name)
{
	if (xmlStrEqual(getElementTag(), name))
		addPendingElement();
	else if (currentProp)
	{
		currentProp->parse(propText);
		currentProp = 0;
	}
}

void XmlTree::elementText(const xmlChar *ch, unsigned len)
{
	if (currentProp)
		propText.append((const char*)ch, len);
}
